package com.xquant.transaction_management_system.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xquant.transaction_management_system.mapper.AccountMapper;
import com.xquant.transaction_management_system.mapper.TransactionMapper;
import com.xquant.transaction_management_system.model.PO.Transaction;
import com.xquant.transaction_management_system.model.PO.Account;
import com.xquant.transaction_management_system.model.PO.TransactionControl;
import com.xquant.transaction_management_system.service.AccountService;
import com.xquant.transaction_management_system.service.TransactionControlService;
import com.xquant.transaction_management_system.service.TransactionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TransactionServiceImpl extends ServiceImpl<TransactionMapper, Transaction> implements TransactionService {
    @Autowired
    private TransactionControlService transactionControlService;
    @Override
    public void getByAccountAndDecode(String transactionAccount, String debentureCode, Double priceDeviation) {
        QueryWrapper<Transaction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("transaction_account",transactionAccount);
        queryWrapper.eq("debenture_code",debentureCode);
        List<Transaction> transactions =
                baseMapper.selectList(queryWrapper);
        if (transactions==null){
            return;
        }
        for (Transaction transaction : transactions) {
            System.out.println(transaction);
            if (transaction.getRealDeviation()>priceDeviation){
                TransactionControl transactionControl = new TransactionControl();
                BeanUtils.copyProperties(transaction,transactionControl);
                transactionControl.setPriceDeviation(transaction.getRealDeviation());
                transactionControlService.save(transactionControl);
            }
        }
    }

    @Override
    public IPage<Transaction> getTranByPages(Page<Transaction> pageParam,String transactionAccount) {
        QueryWrapper<Transaction> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("transaction_date");
        if ("admin".equals(transactionAccount)){
            IPage<Transaction> page = baseMapper.selectPage(pageParam, queryWrapper);
            return page;
        }
        queryWrapper.eq("transaction_account",transactionAccount);
        IPage<Transaction> page = baseMapper.selectPage(pageParam, queryWrapper);
        return page;
    }
}
